import { useReducer } from 'react';

export interface IRoleAuthorizedStateType {
  seachValueLeft: string; //左侧树搜索输入的值
  treeSelectKeys: string[]; //选中的值
  treeExpandedKeys: string[]; //展开的值
  autoExpandParent: boolean; //左侧树是否自动展开
  treeCheckRows: any[]; //左侧树选中的row
  seachValueRight: string;
  checkLengthData: number;
  leftTreeData: any[];
}

interface IActionType {
  type:
    | 'auth-seachValueLeft'
    | 'auth-treeSelectKeys'
    | 'auth-treeExpandedKeys'
    | 'auth-autoExpandParent'
    | 'auth-treeCheckRows'
    | 'auth-seachValueRight'
    | 'auth-checkLengthData'
    | 'auth-leftTreeData'
    | 'remove-all-state';
  payload: any;
}

export const useAuthorizedReducer = () => {
  const initState: IRoleAuthorizedStateType = {
    seachValueLeft: '',
    treeSelectKeys: [],
    treeExpandedKeys: [],
    autoExpandParent: false,
    treeCheckRows: [],
    seachValueRight: '',
    checkLengthData: 0,
    leftTreeData: [],
  };

  const reducer = (state: IRoleAuthorizedStateType, actions: IActionType) => {
    switch (actions?.type) {
      case 'auth-seachValueLeft':
        return {
          ...state,
          seachValueLeft: actions?.payload,
        };
      case 'auth-treeSelectKeys':
        return {
          ...state,
          treeSelectKeys: actions?.payload,
        };
      case 'auth-treeExpandedKeys':
        return {
          ...state,
          treeExpandedKeys: actions?.payload,
        };
      case 'auth-autoExpandParent':
        return {
          ...state,
          autoExpandParent: actions?.payload,
        };
      case 'auth-treeCheckRows':
        return {
          ...state,
          treeCheckRows: actions?.payload,
        };
      case 'auth-seachValueRight':
        return {
          ...state,
          seachValueRight: actions?.payload,
        };
      case 'auth-checkLengthData':
        return {
          ...state,
          checkLengthData: actions?.payload,
        };
      case 'auth-leftTreeData':
        return {
          ...state,
          leftTreeData: actions?.payload,
        };
      case 'remove-all-state': {
        return {
          ...state,
          ...initState,
        };
      }
      default:
        return {
          ...state,
        };
    }
  };
  const [state, roleAuthDispatch] = useReducer(reducer, initState);
  return {
    ...state,
    roleAuthDispatch,
  };
};
